package com.mashiro.system.controller;

import com.mashiro.common.annotation.Log;
import com.mashiro.common.core.controller.BaseController;
import com.mashiro.common.core.domain.AjaxResult;
import com.mashiro.common.core.domain.entity.SysRole;
import com.mashiro.common.core.domain.model.LoginUser;
import com.mashiro.common.core.page.TableDataInfo;
import com.mashiro.common.enums.BusinessType;
import com.mashiro.common.utils.SecurityUtils;
import com.mashiro.common.utils.poi.ExcelUtil;
import com.mashiro.system.domain.MoeWorkOrder;
import com.mashiro.system.service.IMoeWorkOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 工单管理Controller
 *
 * @author mashiro
 * @date 2023-04-14
 */
@RestController
@RequestMapping("/system/order")
public class MoeWorkOrderController extends BaseController {
    @Resource
    private IMoeWorkOrderService moeWorkOrderService;

    /**
     * 查询工单管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(MoeWorkOrder moeWorkOrder) {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        AtomicBoolean isAdmin = new AtomicBoolean(false);
        AtomicBoolean isManager = new AtomicBoolean(false);
        roles.forEach(sysRole -> {
            if (11L == sysRole.getRoleId()){
                isManager.set(true);
            }else if (1L == sysRole.getRoleId() || 10L == sysRole.getRoleId()){
                isAdmin.set(true);
            }
        });
        if (!isAdmin.get()){
            if (isManager.get()){
                moeWorkOrder.setReceiverId(SecurityUtils.getUserId());
            }
        }
        List<MoeWorkOrder> list = moeWorkOrderService.selectMoeWorkOrderList(moeWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出工单管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "工单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MoeWorkOrder moeWorkOrder) {
        List<MoeWorkOrder> list = moeWorkOrderService.selectMoeWorkOrderList(moeWorkOrder);
        ExcelUtil<MoeWorkOrder> util = new ExcelUtil<MoeWorkOrder>(MoeWorkOrder.class);
        util.exportExcel(response, list, "工单管理数据");
    }

    /**
     * 获取工单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(moeWorkOrderService.selectMoeWorkOrderById(id));
    }

    /**
     * 新增工单管理
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MoeWorkOrder moeWorkOrder) {
        return toAjax(moeWorkOrderService.insertMoeWorkOrder(moeWorkOrder));
    }

    /**
     * 修改工单管理
     */
    @PreAuthorize("@ss.hasAnyPermi('system:order:edit,system:order:business')")
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MoeWorkOrder moeWorkOrder) {
        return toAjax(moeWorkOrderService.updateMoeWorkOrder(moeWorkOrder));
    }

    /**
     * 删除工单管理
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "工单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(moeWorkOrderService.deleteMoeWorkOrderById(id));
    }
}
